<?php

namespace My\Controller;

use My\Controller\MyController;

/**
 * 我的学习-最近学习
 */
class MyStudyController extends MyController {

    //学过的课程列表
    public function learnCurriculumList() {
        $iUserId = $this->iUserId;
        //每个学过的课程中，已学的课时
        $learnList = M()->table("qy_user_period_record upr")
                ->field("max(upr.create_time) learn_time,count(DISTINCT upr.period_id) as learned_period_count,upr.curriculum_id,cu.curriculum_id,cu.curriculum_img,cu.curriculum_name,cu.description")
                ->join("qy_period as p ON p.period_id = upr.period_id AND p.status=1")
                ->join("qy_curriculum as cu ON cu.curriculum_id = upr.curriculum_id")
                ->where(array('upr.user_id' => $iUserId))
                ->group('upr.curriculum_id')
                ->order('learn_time DESC')
                ->select();

        if (empty($learnList)) {
            $this->returnList($learnList);
        }
        foreach ($learnList as &$val) {
            $val['curriculum_img'] = C('ADMIN_URL') . $val['curriculum_img'];
            $learn_curriculum_ids[] = $val['curriculum_id'];    //学习过的课时所属课程id
        }
        unset($val);
        //统计每个课程里面所有课时
        $learnPeriodDataList = M("Period")->field("count(period_id) as period_id_count,curriculum_id")->where(array('curriculum_id' => array('in', $learn_curriculum_ids), 'status' => 1))->group('curriculum_id')->select();

        foreach ($learnList as &$val) {
            foreach ($learnPeriodDataList as $vv) {
                if ($vv['curriculum_id'] == $val['curriculum_id']) {
                    $val['period_id_count'] = $vv['period_id_count'];
                    $val['learn_rate'] = (number_format($val['learned_period_count'] / $vv['period_id_count'], 3) * 100) . "%";
                }
            }
        }
        unset($val);
        $this->returnList($learnList);
    }

    //学过的章节课时-课程的章节课时列表
    public function chapterPeriodList() {
        $iCurriculumId = I('curriculum_id', 0, 'intval');
        //已学过的课时
        $arrWhere['user_id'] = $this->iUserId;
        $arrWhere['curriculum_id'] = $iCurriculumId;
        $periodRecList = M('UserPeriodRecord')->field('DISTINCT period_id')->where($arrWhere)->select();
        empty($periodRecList) && $this->returnList($periodRecList);

        //全部章节课时
        $chapterList = M('Chapter')->field('chapter_id,chapter_name')->where(array('curriculum_id' => $iCurriculumId, 'status' => 1))->order('sort ASC')->select();
        $periodList = M('Period')->field('period_id,chapter_id,period_name,video_duration,period_desc')->where(array('curriculum_id' => $iCurriculumId, 'status' => 1))->select();
        foreach ($periodList as &$val) {  //将学过的标记learned =1 
            $val['learned'] = 0;
            foreach ($periodRecList as $vv) {
                if ($val['period_id'] == $vv['period_id']) {
                    $val['learned'] = 1;
                }
            }
        }
        unset($val);

        foreach ($chapterList as &$val) {
            foreach ($periodList as $vv) {
                if ($val['chapter_id'] == $vv['chapter_id']) {
                    $val['period_list'][] = $vv;
                }
            }
        }
        unset($val);
        $this->returnList($chapterList);
    }

}
